#pragma once

#include "pch.h"
#include "Field.h"

struct TBInfo
{
	std::string m_TableName;
};

struct TBGetInfo :public TBInfo
{
	std::vector<Field> m_Fields;
	static std::string SqlTemp()
	{
		std::string temp = R"(
SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_KEY, COLUMN_DEFAULT, EXTRA
FROM information_schema.COLUMNS
WHERE table_name = '{{{m_TableName}}}';
)";
		return temp;
	}
	std::string Sql() const
	{
		kainjow::mustache::mustache m1 = SqlTemp();
		kainjow::mustache::data d1;
		d1.set("m_TableName", m_TableName);
		return m1.render(d1);
	}

};
struct TBCreateInfo :public TBInfo
{
	std::vector<Field> m_Fields;
	void AddField(const Field& inst)
	{
		m_Fields.push_back(inst);
	}

	static std::string SqlTemp()
	{
		std::string temp = R"(
CREATE TABLE IF NOT EXISTS {{{m_TableName}}} (
{{#m_Fields}} 
	{{{m_Name}}} {{{m_TypeText}}} {{{m_PrimaryKey}}}  {{{m_AutoInc}}} {{{m_NotNull}}} {{{m_Default}}} {{{m_Sep}}}
{{/m_Fields}})
)";
		return temp;
	}
	std::string Sql() const
	{
		kainjow::mustache::mustache m1 = SqlTemp();
		kainjow::mustache::data d1;
		d1.set("m_TableName", m_TableName);
		kainjow::mustache::list l1;

		int numFields = m_Fields.size();

		for (int i = 0; i < numFields; i++)
		{
			const auto& field = m_Fields[i];
			kainjow::mustache::data d2;
			d2.set("m_Name", field.m_Name);
			d2.set("m_TypeText", field.GetTypeText());

			if (field.m_Default.size())
			{
				d2.set("m_Default", field.m_Default);
			}

			if (field.m_AutoInc)
			{
				d2.set("m_AutoInc", "AUTO_INCREMENT");
			}
			if (field.m_PrimaryKey)
			{
				d2.set("m_PrimaryKey", "PRIMARY KEY ");
			}
			if (field.m_NotNull)
			{
				d2.set("m_NotNull", "NOT NULL");
			}
			if (i != numFields - 1)
			{
				d2.set("m_Sep", ",");
			}

			l1.push_back(d2);
		}

		d1.set("m_Fields", l1);
		return m1.render(d1);
	}
};

struct TBDropInfo :public TBInfo
{
	static std::string SqlTemp()
	{
		return R"(DROP TABLE {{{m_TableName}}})";
	}
	std::string Sql() const
	{
		kainjow::mustache::mustache m1 = SqlTemp();
		kainjow::mustache::data d1;
		d1.set("m_TableName", m_TableName);
		return m1.render(d1);
	}
};